package www.houdoudou.com.customer;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import www.houdoudou.com.common.CommonFunction;
import www.houdoudou.com.common.CommonMathFunction;
import www.houdoudou.com.common.ConnFactory;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class SearchMachine {
	
	
	
      public static JSONObject  search(String jsonData){
        	   
    	  JSONObject result = new JSONObject();
    		JSONObject jsonfrom= JSONObject.fromObject(jsonData);				
    		//判断请求的功能d
    		int FunctionCode=jsonfrom.getInt("FunctionCode");
    		   PreparedStatement pstm=null; 
    		   Statement  stm=null;
    		   ResultSet rs=null; 	
    		   String CurrentConnUser=CommonMathFunction.genRandomNum(10);
    		   Connection conn=ConnFactory.getCurrentConnection(CurrentConnUser);
    		   switch(FunctionCode){
    		   case 1:{
    			   try {
				 		 
    				   String sql = "select * from Tb_Machine";  
    					stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
    					rs= stm.executeQuery(sql);
    					JSONArray Machines= new JSONArray();
    					while(rs.next()){
    						JSONObject machine= new JSONObject();
    						boolean isadd=true;   					
    					if(isadd){
    						machine.put("Machine_Address", rs.getString("Machine_Address"));
                            machine.put("Machine_ID", rs.getInt("Machine_ID"));
                            machine.put("Machine_Name", rs.getString("Machine_Name"));
                            double Distance= CommonMathFunction.GetDistance(rs.getDouble("Machine_Longitude"), rs.getDouble("Machine_Latitude"), jsonfrom.getDouble("Longitude"), jsonfrom.getDouble("Latitude"));
                            String strD = String.valueOf(Math.abs(Distance));
                            String[] strs = strD.split("\\.");
                            int d=Integer.parseInt(strs[0]);
                            if(d>1000){
                            	int a=d/1000;
                            	int b=d%1000;
                            	 machine.put("Distance", "约"+a+" "+b+"米");
                            }else{
                            machine.put("Distance", "约"+strs[0]+"米");}
                            
                             Machines.add(machine);               
    					                         }
    					
    					}
    					result.put("ActionStatus", true);
    					result.put("ErrorInfo", "成功");
    					result.put("Machines", Machines);
    			   
    			  }catch (SQLException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    					result.put("ActionStatus", false);
    					result.put("ErrorInfo", "查询失败");
    				}finally{
    					ConnFactory.closeConn(CurrentConnUser, pstm, stm, rs);
    				}
    			   
    		                 break;} 
    		   case 2:{
    			   try {
				 		 String Search_Content=jsonfrom.getString("Search_Content");
				 		 String sql;
				 		 boolean  isMachine_ID=false;
				 		 int Machine_ID = 0;
				 		 if(CommonMathFunction.isNumeric(Search_Content)){
				 			 try{
				 				 Machine_ID= Integer.parseInt(Search_Content);
				 				 isMachine_ID=true;
				 			 }catch(Exception e){
				 				isMachine_ID=false;
				 			 }
				 		 }
				 		 if(isMachine_ID){
    				      sql = "select * from Tb_Machine where Machine_ID ="+Machine_ID;
    				      stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
      					  rs= stm.executeQuery(sql);
    				   }else{
    					   sql = "select * from Tb_Machine where Machine_Address like ?"; 			 		 
    					pstm=conn.prepareStatement(sql);
    					 pstm.setString(1,"%"+Search_Content+"%");
    					rs= pstm.executeQuery(); }
				 		 
    					JSONArray Machines= new JSONArray();
    					while(rs.next()){
    						JSONObject machine= new JSONObject();
    						boolean isadd=true;   					
    					if(isadd){
    						machine.put("Machine_Address", rs.getString("Machine_Address"));
                            machine.put("Machine_ID", rs.getInt("Machine_ID"));
                            machine.put("Machine_Name", rs.getString("Machine_Name"));
                            double Distance= CommonMathFunction.GetDistance(rs.getDouble("Machine_Longitude"), rs.getDouble("Machine_Latitude"), jsonfrom.getDouble("Longitude"), jsonfrom.getDouble("Latitude"));
                            String strD = String.valueOf(Math.abs(Distance));
                            String[] strs = strD.split("\\.");
                            int d=Integer.parseInt(strs[0]);
                            if(d>1000){
                            	int a=d/1000;
                            	int b=d%1000;
                            	 machine.put("Distance", "约"+a+" "+b+"米");
                            }else{
                            machine.put("Distance", "约"+strs[0]+"米");}
                            
                             Machines.add(machine);               
    					                         }
    					
    					}
    					result.put("ActionStatus", true);
    					result.put("ErrorInfo", "成功");
    					result.put("Machines", Machines);
    			   
    			  }catch (SQLException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    					result.put("ActionStatus", false);
    					result.put("ErrorInfo", "查询失败");
    				}finally{
    					ConnFactory.closeConn(CurrentConnUser, pstm, stm, rs);
    				}
        		                 break;}
    		   }// end of switch
    		   
    		   
    		   
    		   return result;
      }
	
}
